package com.zx.service.impl.dj;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import com.zx.dao.dj.PriceRecordMapper;
import com.zx.model.dj.PriceRecord;
import com.zx.service.dj.PriceManageService;
import com.zx.utils.DateUtils;

/**
 * 
 * @author yt
 *
 */
@Service
public class PriceManageServiceImpl implements PriceManageService {

	@Autowired
	private PriceRecordMapper priceRecordMapper;

	/**
	 * 入库
	 */
	public void uptDiscount(Model mod, String smodel, String scolor, BigDecimal lastprice, BigDecimal sdiscount,
			BigDecimal nowprice) {

		if (sdiscount == null) {
			List<PriceRecord> list = priceRecordMapper.select(smodel, scolor);
			mod.addAttribute("list", list);
		} else {
			BigDecimal lp = new BigDecimal(0.0);
			List<PriceRecord> list1 = priceRecordMapper.select(smodel, scolor);
			for (int i = 0; i < list1.size(); i++) {
				PriceRecord prd=list1.get(i);
				lp = prd.getNowprice();
				}
			PriceRecord pr = new PriceRecord();
			BigDecimal p = new BigDecimal(0.0);
			pr.setModel(smodel);
			pr.setColor(scolor);
			pr.setLastprice(lp);
			pr.setDiscount(sdiscount);
			p = sdiscount.multiply(lp);
			pr.setNowprice(p);
			pr.setUpdateTime(DateUtils.getCurrentTime());
			priceRecordMapper.update(pr);
			List<PriceRecord> list2 = priceRecordMapper.select(smodel, scolor);
			mod.addAttribute("list", list2);
		}

	}

	public void uptSpecial(Model mod, String pmodel, String pcolor,
			BigDecimal pnowprice) {

		if (pnowprice == null) {
			List<PriceRecord> list = priceRecordMapper.select(pmodel, pcolor);
			mod.addAttribute("list", list);
		} else {
			BigDecimal lp = new BigDecimal(0.0);
			List<PriceRecord> list1 = priceRecordMapper.select(pmodel, pcolor);
			for (int i = 0; i < list1.size(); i++) {
				PriceRecord prd=list1.get(i);
				lp = prd.getNowprice();
				}
			PriceRecord pr = new PriceRecord();
			pr.setModel(pmodel);
			pr.setColor(pcolor);
			pr.setLastprice(lp);
			pr.setNowprice(pnowprice);
			pr.setUpdateTime(DateUtils.getCurrentTime());
			priceRecordMapper.update(pr);
			List<PriceRecord> list2 = priceRecordMapper.select(pmodel, pcolor);
			mod.addAttribute("list", list2);
		}
	}


}